$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'biz/room/list',
        datatype: "json",
        colModel: [			
			{ label: '游戏大厅', name: 'centerName', index: 'center_id', width: 80}, 			
			{ label: '房间ID', name: 'id', index: 'id', width: 50, key: true},
			{ label: '房间名', name: 'roomName', index: 'room_name', width: 80 }, 			
			{ label: '发包数量', name: 'packetQty', index: 'packet_qty', width: 50 },
			{ label: '发包金额', name: 'minAmount', index: 'min_amount', width: 50,
				formatter: function(value, options, row) {
					return value + '-' + row.maxAmount;
				}
			},
			{ label: '当前在线', name: 'personOnline', index: 'person_online', width: 60,
				formatter: function(value, options, row) {
					return '人数限制:' + row.personLimit + "人<br/>在线人数:" +  value + "人<br/>真实在线:" + row.realPersonOnline + "人";
				}
			},
			{ label: '机器人盈利', name: 'robotProfit', index: 'robot_profit', width: 60, sortable: false },
			{ label: '启用机器人', name: 'enableRobot', index: 'enableRobot', width: 50,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-danger">禁用</span>';
					} else {
						return '<span class="label label-primary">启用</span>';
					}
				}
			},
			{ label: '机器人配置', name: 'enableRobot', index: 'enableRobot', width: 110, sortable: false,
				formatter: function(value, options, row) {
					if (value == 1) {
						return '发包频率:' + row.robotSendRate + "<br/>抢包频率:" +  row.robotGrabRate + "<br/>机器人:" + row.robotLandmineRate + "%<br/>普通会员:" 
							+ row.memberLandmineRate + "%<br/>最大盈利:" + (row.lossLimit || '无') + "<br/>最大亏损" + (row.profitLimit || '无') + "<br/>时间点:" + (row.pointTime || '无');
					}
					return "";
				}
			},
			{ label: '是否为VIP', name: 'isVip', index: 'is_vip', width: 50,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">普通房间</span>';
					} else {
						return '<span class="label label-danger">VIP房间</span>';
					}
				}
			},
			{ label: 'VIP房间', name: 'vipRoomPwd', index: 'vip_room_pwd', width: 80,
				formatter: function(value, options, row) {
					if (row.isVip == 1) {
						return '房间密码:' + value + "<br/>创建会员:" +  (row.vipMemberId || '无') + "<br/>过期日期:" + row.vipExpireTime;
					}
					return "";
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
			{ label: '房间状态', name: 'status', index: 'status', width: 50,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-danger">禁用</span>';
					} else {
						return '<span class="label label-primary">启用</span>';
					}
				}
			}
        ],
		viewrecords: true,
        height: 500,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        loadComplete:function(xhr) {
        	vm.centerList = xhr.centerList;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		q: { id: "", roomName: ""},
		room: {},
		centerList : null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.room = {status : 1, enableRobot : 0, packetQty: "10@1", isVip: 0, centerId: "", vipRoomPwd: "", personOnline: 0, robotSendRate: 20000, robotGrabRate: 8000, robotLandmineRate: 0, robotQty: 20, memberLandmineRate: 25};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.room.id == null ? "biz/room/save" : "biz/room/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.room),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(id){
			$.get(baseURL + "biz/room/info/"+id, function(r){
                vm.room = r.room;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:vm.q
            }).trigger("reloadGrid");
		},
		changeStatus: function (status) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定操作？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "biz/room/changeStatus",
				    dataType: "json",
				    data: $.param({ids:ids,status:status},true),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		genPwd : function() {
			if(this.room.vipRoomPwd == null || this.room.vipRoomPwd == "") {
				var out = ""; 
				for(var i=0; i < 6; i++) {
					out += Math.floor(Math.random()*10); 
				}
				this.room.vipRoomPwd = out;
			}
		}
	}
});